package com.woke.adapter;

import java.util.ArrayList;
import java.util.List;

import com.zhongcai.online.R;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class Forwhyadter extends BaseAdapter{
	private LayoutInflater mInflater;
	private List<String> datalist;
	private int phonewith;

	public Forwhyadter(LayoutInflater minflater,	ArrayList<String> datalist, int phonewith) {
		this.mInflater = minflater;
		this.datalist = datalist;
		this.phonewith = phonewith;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
			return datalist.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
//	class Holder {
//		ImageView mIicon;
//		RelativeLayout mTbankname;
////		TextView mTbankcarno;
////		TextView mTcardtype;
//	}
	
	public Bitmap setbitmaprsutu(String str){
		BitmapFactory.Options op = new BitmapFactory.Options();
		Bitmap bitmap = null;
//		if (bitmap == null) {
//		    // ���ʵ��ʧ�� ����Ĭ�ϵ�Bitmap����
//		    return defaultBitmapMap;
//		}
		op.inSampleSize = 4; 
		try {
		    // ʵ��Bitmap
		    bitmap = BitmapFactory.decodeFile(str,op);
		} catch (OutOfMemoryError e) {
		    //
//			Toast.makeText(get, "ͼƬ��ȡʧ�ܣ�������", Toast.LENGTH_SHORT).show();
		}
		Bitmap pic = null;
		if(null != bitmap){
//			int width = bitmap.getWidth();
//			int height = bitmap.getHeight();
//			int newWidth = 480;
//			int newHeight = 640;
//			float scaleWidth = ((float) newWidth) / width;
//			float scaleHeight = ((float) newHeight) / height;
//			Matrix matrix = new Matrix();
//			matrix.postScale(scaleWidth, scaleHeight);
//			bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height,
//					matrix, true);
//		op.inSampleSize = 4; 
			pic = bitmap;
		}else{
			pic = null;
		}
		
		return pic;
		
	};

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
//		Holder holder = null;
//		if(convertView == null){
//			holder = new Holder();
//			convertView = mInflater.inflate(R.layout.item_grid_photo, null);
//			holder.mIicon = (ImageView) convertView.findViewById(R.id.item_gridphoto_image);
//			holder.mTbankname = (RelativeLayout) convertView.findViewById(R.id.item_gridphoto_relative);
//			holder.mTbankcarno = (TextView) convertView.findViewById(R.id.item_myblind_cardno);
//			holder.mTcardtype = (TextView) convertView.findViewById(R.id.item_myblind_cardtype);
//			convertView.setTag(holder);
//		}else{
//			 holder = (Holder) convertView.getTag();
//		}
		String string = datalist.get(position);
//		LayoutParams params = holder.mTbankname.getLayoutParams();
//		params.width = phonewith / 3 - 15 ;
//		params.height = phonewith / 3 - 15;
//		holder.mTbankname.setLayoutParams(params);
//		if(string.equals("")){
//			holder.mIicon.setImageResource(R.drawable.avborrowfor_image_photo);
//		}else{
//			LayoutParams params1 = holder.mIicon.getLayoutParams();
//			params1.width = phonewith / 3 - 15;
//			params1.height = phonewith / 3 - 15;
//			Log.e("phonewith / 3 - 15", ""+(phonewith / 3 - 15));
//			holder.mIicon.setLayoutParams(params1);
//			Bitmap setbitmaprsutu = setbitmaprsutu(string);
//			holder.mIicon.setImageBitmap(setbitmaprsutu);
//		}
		
		convertView = mInflater.inflate(R.layout.item_grid_photo, null);
		ImageView mIicon = (ImageView) convertView.findViewById(R.id.item_gridphoto_image);
		ImageView mIicon1 = (ImageView) convertView.findViewById(R.id.item_gridphoto_image1);
		RelativeLayout mTbankname = (RelativeLayout) convertView.findViewById(R.id.item_gridphoto_relative);
		LayoutParams params = mTbankname.getLayoutParams();
		params.width = phonewith / 3;
		params.height = phonewith / 3;
		mTbankname.setLayoutParams(params);
		if(string.equals("")){
			mIicon1.setVisibility(View.GONE);
			mIicon.setImageResource(R.drawable.avborrowfor_image_photo);
		}else{
//			LayoutParams params1 = mIicon.getLayoutParams();
//			params1.width = phonewith / 3 - 15;
//			params1.height = phonewith / 3 - 15;
//			Log.e("phonewith / 3 - 15", ""+(phonewith / 3 - 15));
//			LayoutParams params2 = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
//			mIicon.setLayoutParams(params2);
//			mIicon.setLayoutParams(params1);
			mIicon.setVisibility(View.GONE);
			Bitmap setbitmaprsutu = setbitmaprsutu(string);
			mIicon1.setImageBitmap(setbitmaprsutu);
		}
		
//		Data_bindcarlist list = datalist.get(position);
//		holder.mTbankname.setText(list.card_name);
//		holder.mTbankcarno.setText(list.card_top + " "+"***"+" "+"***"+" "+list.card_last);
//		holder.mTcardtype.setText(list.card_name);
		
		
		return convertView;
	}

}
